package com.yanqu.road.server.logic.activity.condition.specialserver;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.task.TaskConditionInfo;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.task.BaseCondition;
import com.yanqu.road.server.logic.task.BaseTaskData;

import java.math.BigInteger;

public class IceServerAddSatiationCondition extends BaseCondition {

    public IceServerAddSatiationCondition(TaskConditionInfo taskConditionInfo, BaseTaskData baseTaskData, BigInteger value, Object initArgs) {
        super(taskConditionInfo, baseTaskData, value, initArgs);
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.IceServerAddSatiation.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.IceServerAddSatiation.getValue(), handle);
    }

    @Override
    public boolean isCompleted(GamePlayer player) {
        return getValue().compareTo(taskConditionInfo.getParamList().get(0)) >= 0;
    }

    @Override
    protected void changeValue(Object object) {
        long arg = (long)object;
        long newValue = Math.min(taskConditionInfo.getParamList().get(0).longValue(), getValue().longValue() + arg);
        setValue(BigInteger.valueOf(newValue));
    }

    @Override
    public void initValue() {
        setValue(BigInteger.ZERO);
    }
}
